const Router = require('koa-router')
const router = new Router()
const WsjAuth = require('../core/wsjAuth.js')
const RegistryController = require('../controllers/registry')
const LoginController = require('../controllers/login')
const PostController = require('../controllers/post')
const TagController = require('../controllers/tag')
const MarketRegionController = require('../controllers/marketRegion')
const QualificationController = require('../controllers/qualification')
const UserController = require('../controllers/user')
const PasswordController = require('../controllers/password')
const JWT = require('../core/jwt.js')
const dbm = require('../config/dbModels');

router.prefix('/api/be')


//创建用户,注册账号
router.post('/createUser', async (ctx, next) => {
    // console.log('第2路啦啦啦');
    // throw new global.errs.NotFound('没有找到相关文章');
    await next();
}, RegistryController.createUser);
//登录
router.post('/login', async (ctx, next) => {
    // console.log('第2路啦啦啦');
    // throw new global.errs.NotFound('没有找到相关文章');
    await next();
}, LoginController.login);

//新建文章
router.post('/createPost', PostController.createPost);
//获取某篇文章
router.get('/getPost/:postId', PostController.getPost);

//新建标签
router.post('/createTag', TagController.createTag);
//获取某个标签
router.get('/getTag/:tagId', TagController.getTag);


//创建和验证JWT
router.post('/createJWT', JWT.createJWT);
router.get('/verifyJWT', JWT.verifyJWT);

//对用户进行crud
router.get('/crudUser',UserController.rdUser);
router.post('/crudUser',UserController.cuUser);

//对股票市场区域进行crud
router.get('/crudMarketRegion', MarketRegionController.rdMarketRegion);
router.post('/crudMarketRegion', MarketRegionController.cuMarketRegion);

//对权限模块进行crud
router.get('/crudQualification',new WsjAuth(110).w,QualificationController.rdQualification);
router.post('/crudQualification',new WsjAuth(110).w,QualificationController.cuQualification);

//修改自己密码
router.post('/modifypassword', PasswordController.update);


router.get('/test2', async (ctx, next) => {
    var yan = await bcrypt.genSalt(saltRounds);
    var res = await bcrypt.hash(myPlaintextPassword, yan);
    console.log('开始了')
    console.log(yan)
    console.log(res)
    console.log('结束了')
    var res = await bcrypt.compare(myPlaintextPassword, '$2b$10$5htpU4dqtrpKM1YyGw7xiuhBkAswHqbYj2aeZyCQUwM7WpE32YELW');
    console.log('比对结果', res)
    return ctx.body = res;
})


router.post('/test4', async (ctx, next) => {
    /*  dbm.MarketRegion.findByPk(1).then(project => {
 console.log(project)
       }) */
    var r = await dbm.MarketRegion.findAll({
        attributes: ['id', 'name'],
        raw: true
    });
    console.log(r)
    try {
        await dbm.MarketRegion.create({ name: ctx.request.body.region })
    }
    catch (e) {

        //console.log(e.original.sqlMessage)
        return ctx.body = { status: -1, msg: '该标签已存在' }
    }
    return ctx.body = ctx.request.body;
})




router.get('/test', async (ctx, next) => {
  return ctx.body = 123;
});


router.get('/haha', async (ctx) => {
    console.log(ctx.request.body)
    return ctx.body = 99
})

module.exports = router
